<?php
class docs extends MX_Controller
{
    private $limit;
    public function __construct()
    {
        parent::__construct();
        $this->limit = 10;
        $this->load->helper('text');
        $this->load->helper('file');
        $this->load->helper('image');
        $this->load->model('Doc_Categories_Model');
        $this->load->model('Doc_Documents_Model');
        $this->load->model('Doc_Menus_Model');
        
    }
    
    public function index()
    {
        $data = array(
            'title' => 'Tài liệu',
            'meta_des' => 'Tài liệu',
            'meta_key' => 'Tài liệu',
            );
        $this->load->view("templates/master", $data);
    }
    
    public function tags()
    {
        $tag = (int)trim($this->uri->segment(2));
        $docModel = new Doc_Documents_Model();    
        $data = $docModel->doclist_byTag($tag);
        $content = $this->load->view('categorys/tags', array('data' => $data), TRUE);
        $data = array(
            'title' => 'Tài liệu',
            'meta_des' => 'Tài liệu',
            'meta_key' => 'Tài liệu',
            'content' => $content
            );
        
        $this->load->view("templates/tags", $data);
    }
    
    public function main()
    {
        $alias = trim($this->uri->segment(2));
        $mnModel = new Doc_Menus_Model();
        $mid = $mnModel->getIdByAlias($alias);
        
        if ($mid == 0) {
            return redirect(base_url());
        }
        
        $docModel = new Doc_Documents_Model();
        $arrCid = $docModel->getlistCategory($mid);
        $strCid = implode(',', $arrCid); 
        
        $new = $docModel->doclist(1, $strCid);
        
        $view = $docModel->doclist(2, $strCid);
        $hit = $docModel->doclist(3, $strCid);
        $od = isset($new[0]->order) ? $new[0]->order : 0;
        
       
        $tags = $docModel->getTagByDocId($strCid);
        $content = $this->load->view('categorys/list', array(
            'order' => $od,
            'cid' => $strCid,
            'tags' => $tags,
            'data' => $new, 
            'data1' => $view, 
            'data2' => $hit), TRUE);
        
        return $this->load->view("templates/list", array('content' => $content));        
    }
    
    public function catetory()
    {
        $alias = trim($this->uri->segment(2));
        $mnModel = new Doc_Categories_Model();
        $cid = $mnModel->getMenuByAlias($alias);
        
        if ($cid == 0) {
            return redirect(base_url());
        }
        
        $docModel = new Doc_Documents_Model();
        $new = $docModel->doclist(1, $cid);

        $view = $docModel->doclist(2, $cid);
        $hit = $docModel->doclist(3, $cid);
        $od = isset($new[0]->order) ? $new[0]->order : 0;
        $cid = isset($new[0]->cid) ? $new[0]->cid : 0;
       
        $tags = $docModel->getTagByDocId($cid);
        $content = $this->load->view('categorys/list', array(
            'order' => $od,
            'cid' => $cid,
            'tags' => $tags,
            'data' => $new, 
            'data1' => $view, 
            'data2' => $hit), TRUE);
        
        return $this->load->view("templates/list", array('content' => $content));        
    }
    
    
    
    public function ajax_comment($id = 0, $offset = 0)
    {
        $id = (int)$id;
        $offset = (int)$offset;
        $docModel = new Doc_Documents_Model();
        $comment = $docModel->getCommentById($id, $offset);
        $this->load->view('detail/comment_ajax', array('comment' => $comment)); 
    }
    public function detail($alias = '')
    {
        $docModel = new Doc_Documents_Model();
        $id = $docModel->getIdByAlias($alias);
        if ($id == 0) {
            return redirect(base_url());
        }
        $docModel->updateHit($id);
        $result = $docModel->getDocumentById($id);
        $tags = $docModel->getTagByDocId($id);
        $content = $this->load->view('detail/detail', array('result' => $result, 'tags' => $tags), TRUE);
       
        $orderDoc = $docModel->getDocumentByMenu($result->cid);
        $oContent = $this->load->view('detail/tuongtu', array('orderDoc' => $orderDoc), TRUE);
        
        $comment = $docModel->getCommentById($id, 0);
        $ctocal = $docModel->ctotal($id);
        $paging = paging_fo('docs/ajax_comment/'.$id, $ctocal, 2, 4);
        $comment = $this->load->view('detail/comment', array(
            'comment' => $comment, 'paging' => $paging), TRUE); 
        
        $this->load->view('templates/detail', array(
            'comment' => $comment,
            'content' => $content,
            'oContent' => $oContent,
            'result' => $result,
            'cid' => $result->cid,
            'order' => $result->order
            ));
      
    }
    
    
    public function popup_display()
    {
        $this->load->model('util/captcha_model');
        $captcha = new Captcha_Model();
        $imgCaptcha = $captcha->captcha_generate();
        $this->load->view('detail/captcha', array('image' =>  $imgCaptcha));
    }
    
    
    public function captchasubmit()
    {
        $this->load->model('util/captcha_model');
        $captcha = new Captcha_Model();
        $arr = array();
        if (!$captcha->captcha_check()) {
            $imgCaptcha = $captcha->captcha_generate();
            $arr['success'] = 0;
            $arr['captcha'] = $imgCaptcha;
            echo json_encode($arr);
            exit();
        } else {
            $this->load->model('Doc_Comments_Model');
            $commentModel = new Doc_Comments_Model();
            $commentModel->addComment();
            $arr['success'] = 1;
            echo json_encode($arr);
            exit();
        }        
    }
    
    public function maxdownload()
    {
        $content = $this->load->view('detail/maxdownload', array(), TRUE);
        $this->load->view('templates/download', array('content' => $content));
    }
    
    public function download($id)
    {
        if ($this->session->userdata('login')) {
            $memberObj = $this->session->userdata('login');
           
            $this->load->helper('download');
            $this->load->model('Doc_Member_Download_Model');
            $downModel = new Doc_Member_Download_Model();
            $numDown = $downModel->checkdownOnday($memberObj->id);
            if ($numDown >= 5) {
                return redirect(site_url('docs/maxdownload'));
            } else {
                
                $docModel = new Doc_Documents_Model();
                $obj = $downModel->checkDown($id, $memberObj->id);
                if ($obj) {
                    $downModel->addDownload($id, $memberObj->id, $obj->id, $obj->down + 1);
                } else {
                    $downModel->addDownload($id, $memberObj->id);
                }
                $numDownload = $downModel->numDownload_byDocId($id);
                $docModel->updateDownload($id, $numDownload);
                $docInfo = $docModel->getDocumentById($id);
                $filePath = ROOT_DIR_PATH.'/'.$docInfo->doc_file;
                $data = file_get_contents($filePath); // Read the file's contents
                $name = basename($docInfo->doc_file);
                force_download($name, $data); 
            }            
        } else {
            return redirect('/');
        }
    }   
}